# -*- coding: utf-8 -*-

import sqlite3

from . import config


class User:

    def __init__(self, uid=None, uname=None):
        self.uid = uid
        self.uname = None
        self.pwd = None
        self.name = None
        self.group = 0
        if uid:
            with sqlite3.connect(config.DATEBASE) as db:
                cur = db.cursor()
                data = cur.execute(
                    """SELECT * FROM users WHERE uid = ?""",
                        (uid, )).fetchone()
                if not data:
                    raise ValueError("wrong uid")
                self.uid = data[0]
                self.uname = data[1]
                self.pwd = data[2]
                self.name = data[3]
                self.group = data[4]
        elif uname:
            with sqlite3.connect(config.DATEBASE) as db:
                cur = db.cursor()
                data = cur.execute(
                    """SELECT * FROM users WHERE uname = ?""",
                        (uname, )).fetchone()
                if not data:
                    raise ValueError("wrong uname")
                self.uid = data[0]
                self.uname = data[1]
                self.pwd = data[2]
                self.name = data[3]
                self.group = data[4]

    
    @staticmethod
    def search(name=None, uid=None, uname=None):
        if uid:
            with sqlite3.connect(config.DATEBASE) as db:
                cur = db.cursor()
                data = cur.execute(
                    """SELECT * FROM users WHERE uid = ?""",
                        (uid, )).fetchone()
                if not data:
                    return []
                _user = User()
                _user.uid = data[0]
                _user.uname = data[1]
                _user.pwd = data[2]
                _user.name = data[3]
                return [_user]
        elif uname:
            with sqlite3.connect(config.DATEBASE) as db:
                cur = db.cursor()
                data = cur.execute(
                    """SELECT * FROM users WHERE uname = ?""",
                        (uname, )).fetchone()
                if not data:
                    return []
                _user = User()
                _user.uid = data[0]
                _user.uname = data[1]
                _user.pwd = data[2]
                _user.name = data[3]
                return [_user]
        elif name:
            with sqlite3.connect(config.DATEBASE) as db:
                cur = db.cursor()
                _name = "%" + name + "%"
                results = []
                for user in cur.execute(
                    """SELECT * FROM users WHERE name LIKE ?""",
                        (_name, )).fetchall():
                    _user = User()
                    _user.uid = user[0]
                    _user.uname = user[1]
                    _user.pwd = user[2]
                    _user.name = user[3]
                    results.append(_user)
                return results
        else:
            raise ValueError("Need at least one param")

    def register(self):
        with sqlite3.connect(config.DATEBASE) as db:
            cur = db.cursor()
            if cur.execute(
                """SELECT uid FROM users WHERE uname = ?""",
                    (self.uname, )).fetchone():
                return False
            cur.execute(
                """INSERT INTO users VALUES (?, ?, ?, ?, ?)""",
                (None, self.uname, self.pwd, self.name, self.group))
            db.commit()
            self.uid = User().search(uname=self.uname)[0].uid
            return True
